<?php
namespace App\Services;
use App\Model\ConfigModel;
use App\Model\UserWishLog;
use App\Model\UserEnergy;
use App\Model\WishModel;
use App\Model\UserModel;
use App\Model\GroupList;
use App\Model\GroupListLog;
use App\Model\GroupListOrder;
use App\Model\GroupListCite;
use App\Model\UserWalletModel;
use App\Model\UserWalletLogModel;

class Group
{
   /**
    * gid     商品ID
    * curcle  第几轮
    */
	public static function charge($gid,$curcle){
        //商品的详情
        $good = GroupList::where('id',$gid)->first();
        //当前拼团的所有信息
        $group_suc_nums = GroupListLog::where('gid',$gid)->where('curcle',$good['curcle'])->count();
        //拼团成功
        if($good['nums'] == $group_suc_nums){
            //中奖数
            $win_num = rand(1,$good['nums']);
            //查询中奖的  拼团信息
            $win_num_res = GroupListLog::where('num',$win_num)->where('gid',$gid)->where('curcle',$good['curcle'])->first();
            //用户的订单
            $win_num_order = GroupListOrder::where('id',$win_num_res['oid'])->first();
            //用户的信息
            $user = UserModel::where('id',$win_num_res['uid'])->first();
            if(!$user){
                return ;
            }
            //用户的资产，增加股票，生成订单
            $user_wallet = UserWalletModel::where('uid',$user['id'])->first();
            
            //增加拼团历史记录
            GroupListCite::create([
                'gid' => $good['id'],
                'gname' => $good['name'],
                'gpic' => $good['picname'],
                'cost' => $good['cost'],
                'sell' => $good['sell'],
                'charge' => $good['charge'],
                'shares' => $good['shares'],
                'start' => $good['start'],
                'end' => $good['end'],
                'nums' => $good['nums'],
                'site' => $win_num,
                'curcle' => $good['curcle'],
                'attr' => $good['attr'],
                'state' => 1,
            ]);
            //增加资产
            if($good['shares'] > 0){
                //增加日志
                UserWalletLogModel::create([
                    'uid' => $user['id'],
                    'num' => $good['shares'],
                    'sign' => '+',
                    'msg' => '拼团成功',
                    'befor' => $user_wallet['shares'],
                    'after' => $user_wallet['shares'] + $good['shares'],
                    'type' => 3,
                    'status' => 17,
                    'dy_table' => 'group_list_log',
                    'dy_id' => $win_num_res['id'],
                ]);
                UserWalletModel::where('uid',$user['id'])->increment('shares',$good['shares']);
            }
            
            //更改订单状态
            GroupListLog::where('id',$win_num_res['id'])->update(['state'=>3]);
            GroupListOrder::where('id',$win_num_res['oid'])->update(['state'=> 1]);
            GroupList::where('id',$win_num_res['gid'])->update(['switch'=> 2]);

            //查询主订单
            $due_main_order = GroupListOrder::where('gid',$good['id'])->where('curcle',$good['curcle'])->get();
            if(count($due_main_order)>0){
                foreach($due_main_order as $k=>$v){
                    $user = UserModel::where('id',$v['uid'])->first();
                    
                    //计算返还用户的利润
                    $charge = GroupListLog::where('oid',$v['id'])->where('state',1)->sum('charge');
                    //计算本金
                    $sell = GroupListLog::where('oid',$v['id'])->where('state',1)->sum('sell');
                    
                    if($charge > 0){
                        UserWalletModel::handleAccount($user['id'],$charge,'+','拼单成功，未中奖。赠送现金',1,19,'group_list_order',$v['id']);
                    }
                    if($sell > 0){
                        //增加日志
                        if($v['paytype'] == 1 || $v['paytype'] == 2){
                            UserWalletModel::handleAccount($user['id'],$sell,'+','拼单成功，未中奖。返还现金',$v['paytype'],18,'group_list_order',$v['id']);
                        }
                    }
                    GroupListlog::where('oid',$v['id'])->where('curcle',$good['curcle'])->where('state',1)->update(['state'=>2]);
                    GroupListOrder::where('id',$v['id'])->update(['state'=>1]);
                }
            }
        }
    }
}